package dao;

import db.DBUtil;
import model.Customer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 顾客数据访问类，操作顾客数据库
 */
public class CustomerDAO {
    
    /**
     * 注册新顾客
     */
    public boolean registerCustomer(Customer customer) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO customers (customer_id, name, phone, is_member, member_points, password) " +
                         "VALUES (?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, customer.getCustomerId());
            pstmt.setString(2, customer.getName());
            pstmt.setString(3, customer.getPhone());
            pstmt.setBoolean(4, customer.isMember());
            pstmt.setDouble(5, customer.getMemberPoints());
            pstmt.setString(6, customer.getPassword());
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("注册顾客失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 顾客登录
     */
    public Customer login(String customerId, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Customer customer = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM customers WHERE customer_id = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, customerId);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                customer = new Customer();
                customer.setCustomerId(rs.getString("customer_id"));
                customer.setName(rs.getString("name"));
                customer.setPhone(rs.getString("phone"));
                customer.setMember(rs.getBoolean("is_member"));
                customer.setMemberPoints(rs.getDouble("member_points"));
                customer.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            System.err.println("顾客登录查询失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        return customer;
    }
    
    /**
     * 升级为会员
     */
    public boolean upgradeToMember(String customerId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE customers SET is_member = true WHERE customer_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, customerId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("升级为会员失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 更新会员积分
     */
    public boolean updateMemberPoints(String customerId, double points) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE customers SET member_points = member_points + ? WHERE customer_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setDouble(1, points);
            pstmt.setString(2, customerId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("更新会员积分失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
    